var _mm = require('util/mm.js');
var _address = require('service/address-service.js');
// 用于地址二级联动
var _cities = require('util/cities/index.js');
// 弹窗模板
var template = require('./address-modal.string');

var addressModel = {
  show: function(option) {
    // option的绑定
    this.option = option;
    this.option.data = this.option.data || {};
    this.$modalWrap = $('.modal-wrap');
    // 渲染页面
    this.loadModal();
    // 绑定事件
    this.bindEvent();
  },
  hide: function() {
    this.$modalWrap.empty();
  },
  loadModal: function() {
    var addressModelHtml = _mm.renderHtml(template, {
      isUpdate: this.option.isUpdate,
      data    : this.option.data
    });
    this.$modalWrap.html(addressModelHtml);
    // 加载省份
    this.loadProvince();
    // 加载城市，city有默认值 -请选择
    // this.loadCities();
  },
  loadProvince: function() {
    var province = _cities.getProvince() || [],
        $provinceSelect = this.$modalWrap.find('#receiver-province');
    $provinceSelect.html(this.getSelectOption(province));
    // 是编辑状态则进行回填
    if(this.option.isUpdate && this.option.data.receiverProvince)  {
      $provinceSelect.val(this.option.data.receiverProvince);
      this.loadCities(this.option.data.receiverProvince);
    }
  },
  // 加载城市信息
  loadCities: function(province) {
    var cities = _cities.getCities(province) || [],
        $city = this.$modalWrap.find('#receiver-city');
    $city.html(this.getSelectOption(cities));
    if(this.option.isUpdate && this.option.data.receiverCity)  {
      $city.val(this.option.data.receiverCity);
    }
  },
  getSelectOption: function(arr) {
    var html = '<option value="">请选择</option>';
    // 获取select框的选项，输入是一个数组,输出是html
    arr.forEach(function(item) {
      html += '<option value='+item +'>'+ item +'</option>';
    });
    return html;
  },
  bindEvent: function() {
    var _this = this;
    this.$modalWrap.find('#receiver-province').change(function() {
      var selectedProvince = $(this).val();
      _this.loadCities(selectedProvince);
    });

    this.$modalWrap.find('.address-btn').click(function() {
      var receiverInfo = _this.getReceiverInfo(),
          isUpdate = _this.option.isUpdate;
      // 使用地址新地址并且 验证通过
      if(!isUpdate && receiverInfo.status) {
          _address.addAddress(receiverInfo.data, function(res) {
            _mm.successTip('地址添加成功');
            _this.hide();
            typeof _this.option.onSuccess === 'function' && _this.option.onSuccess(res);
          }, function(errMsg) {
            _mm.errorTip(errMsg);
          });

      }
      else if(isUpdate && receiverInfo.status)  {
          _address.updateAddress(receiverInfo.data, function(res) {
            _mm.successTip('地址修改成功');
            _this.hide();
            typeof _this.option.onSuccess === 'function' && _this.option.onSuccess(res);
          }, function(errMsg) {
            _mm.errorTip(errMsg);
          });

      }
      // 验证不通过
      else if(!receiverInfo.status) {
        _mm.errorTip(receiverInfo.errMsg || '好像哪里不对了~');
      }

    });
    // 保证点击modal内容区不关闭弹窗
    this.$modalWrap.find('.modal-con').click(function(e) {
      e.stopPropagation();
    });
    // 点击x或点击蒙版区关闭modal
    this.$modalWrap.find('.close').click(function() {
      _this.hide();
    });
  },
  // 表单里收件人信息，表单验证
  getReceiverInfo: function() {
    var receiverInfo = {},
        result = {
          status: false
        };
    if(this.option.isUpdate) {
      receiverInfo.id = this.option.data.id;
    }
    receiverInfo.receiverName = $.trim(this.$modalWrap.find('#receiver-name').val());
    receiverInfo.receiverProvince = this.$modalWrap.find('#receiver-province').val();
    receiverInfo.receiverCity = this.$modalWrap.find('#receiver-city').val();
    receiverInfo.receiverAddress= $.trim(this.$modalWrap.find('#receiver-address').val());
    receiverInfo.receiverPhone= $.trim(this.$modalWrap.find('#receiver-phone').val());
    receiverInfo.receiverZip= $.trim(this.$modalWrap.find('#receiver-zip').val());
    if(!receiverInfo.receiverName) {
      result.errMsg = '请输入收件人姓名'
    }
    else if(!receiverInfo.receiverProvince) {
      result.errMsg = '请选择收件人所在省份'
    }
    else if(!receiverInfo.receiverCity) {
      result.errMsg = '请选择收件人所在城市'
    }
    else if(!receiverInfo.receiverAddress) {
      result.errMsg = '请输入收件人详细地址'
    }
    else if(!receiverInfo.receiverPhone) {
      result.errMsg = '请输入收件人手机'
    }
    // 验证通过
    else {
      result.status = true;
      result.data = receiverInfo;
    }
    return result;
  }
}
module.exports = addressModel;